/**
 * Created by king on 2017/6/22.
 */
(function() {
    ADF.actionModel.XYDistributeVerticallyModel = function() {
        ADF.actionModel.XYDistributeVerticallyModel.superclass.constructor.call(this);
        this.config = {text:"垂直分布",icon:"icon-shuipingfenbu"};
        this.id = "XYDistributeVertically";
        this.hotkeyInfo = {
            // type : "keydown",
            // key : "ArrowRight"
        }
    };

    COM.util.defineClass(ADF.actionModel.XYDistributeVerticallyModel, AGEF.ActionModel, {
        execute: function(graphicalViewer){
            var selection = graphicalViewer.getSelection();
            
            var topMin = null;
            var bottomMax = null;

            var topMinMiddle = null;
            var bottomMaxMiddle = null;
            var totalMiddleHeight = null;
            
            for(var i in selection){
                var selectionItem = selection[i];
                var bounds = selectionItem.model.getLayoutData();
                if(bounds instanceof COM.draw2d.Rectangle){
                    if(!topMin || (topMin && bounds.y < topMin)){
                        topMin = bounds.y;
                    }

                    if(!bottomMax || (bottomMax && bounds.y + bounds.height > bottomMax)){
                        bottomMax = bounds.y + bounds.height;
                    }
                }
            }

            if(topMin && bottomMax){
                topMinMiddle = topMin + selection[0].model.getLayoutData().height / 2;
                bottomMaxMiddle = bottomMax - selection[selection.length - 1].model.getLayoutData().height / 2;
                totalMiddleHeight = bottomMaxMiddle - topMinMiddle;
            }

            if(topMinMiddle && bottomMaxMiddle && totalMiddleHeight){
                var map = {};
                var cmd = COM.gef.Command.createCommand({
                    execute: function execute() {
                        for(var i in selection){
                            if(i == 0 || i == selection.length -1){
                                continue;
                            }
                            var selectionItem = selection[i];
                            var model = selectionItem.model;
                            var bounds = model.getLayoutData();
                            map[model.id] = bounds;
                            model.setLayoutData(new COM.draw2d.Rectangle(bounds.x, topMinMiddle + totalMiddleHeight / (selection.length - 2 + 1) * i - bounds.height / 2, bounds.width, bounds.height));
                        }
                    },
                    undo: function undo() {
                        for(var i in selection){
                            if(i == 0 || i == selection.length -1){
                                continue;
                            }
                            var selectionItem = selection[i];
                            var model = selectionItem.model;
                            model.setLayoutData(map[model.id]);
                        }
                    }
                });

                var commands = new COM.gef.CompoundCommand();
                commands.add(cmd);
                graphicalViewer.getEditDomain().getCommandStack().execute(commands);
            }
        },
        getEnabledState : function(graphicalViewer){
            var selection = graphicalViewer.getSelection();
            if(selection && selection.length > 2){
                var parent = null;
                for(var i in selection){
                    var selectionItem = selection[i];
                    if((parent && selectionItem.parent != parent) || !(selectionItem.model.getLayoutData() instanceof COM.draw2d.Rectangle)){
                        return false;
                    }else{
                        parent = selectionItem.parent;
                    }
                }
                return true;
            }else{
                return false;
            }
        }
    });
})();
